﻿Public Class FormCustomer

    Private Sub FormCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GUIHelper.FormatDataGridView(dgvCustomer)
        LoadData()
    End Sub

    Public Sub LoadData()
        Me.dgvCustomer.DataSource = Sql.ExecuteDataTable("SELECT * FROM Customer WHERE (CustomerName LIKE N'%" & txtSearch.Text & "%' OR Phone LIKE N'%" & txtSearch.Text & "%' OR Address LIKE N'%" & txtSearch.Text & "%') AND Active=1 ")
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.dgvCustomer.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("are you sure want to remove ?", "remove", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = dgvCustomer.SelectedRows(0)
        Dim id = row.Cells("CustomerId").Value

        Sql.ExecuteNonQuery("UPDATE Customer SET Active=0 WHERE CustomerId=" & id)

        LoadData()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New FormCustomerEditor
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        If Me.dgvCustomer.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row As DataGridViewRow = dgvCustomer.SelectedRows(0)
        Dim CustomerId = row.Cells("CustomerId").Value
        Dim CustomerName = row.Cells("CustomerName").Value
        Dim Phone = row.Cells("Phone").Value
        Dim Address = row.Cells("Address").Value

        Dim frm As New FormCustomerEditor()
        frm.txtCustomerId.Enabled = False
        frm.txtCustomerId.Text = CustomerId
        frm.txtCustomerName.Text = CustomerName
        frm.txtPhone.Text = Phone
        frm.txtAddress.Text = Address

        Dim photo = Sql.ExecuteScalar("SELECT Photo FROM Customer WHERE CustomerId = " & CustomerId)
        If photo Is DBNull.Value Then
            frm.PictureBox1.Image = Nothing
        Else
            Dim bytes As Byte() = DirectCast(photo, Byte())
            Dim m As New System.IO.MemoryStream(bytes)
            frm.PictureBox1.Image = New System.Drawing.Bitmap(m)
        End If
        

        If row.Cells("Email").Value Is DBNull.Value Then
            frm.txtEmail.Text = ""
        Else
            frm.txtEmail.Text = row.Cells("Email").Value

        End If

        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    
End Class